<?php
/**
 *	Logic of Licencee, Locations and Companies.
 *	@category		cmTools
 *	@package		BugTracker.Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			0.1
 *	@version		$Id$
 */
/**
 *	Logic of Licencee, Locations and Companies.
 *	@category		cmTools
 *	@package		BugTracker.Logic
 *	@extends		CMF_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			0.1
 *	@version		$Id$
 */
class Logic_Project extends CMF_Krypton_Core_Logic
{
	public static function addProject( $data, $prefix = "" )
	{
		$data	= self::removePrefixFromFields( $data, $prefix );
		self::validateForm( 'administration/project', 'addProject', $data, $prefix );
		$data['created']	= time();
		$model	= new Model_Project();
		return $model->insert( $data );
	}
		
	public static function editProject( $projectId, $data, $prefix = "" )
	{
		if( !$projectId )
		 	throw new CMF_Krypton_Exception_Logic( 'projectId_missing' );
		$model	= new Model_Project( $projectId );
		if( !$model->exists() )
			throw new CMF_Krypton_Exception_Logic( 'invalid_project_id' );
		$data	= $this->removePrefixFromFields( $data, $prefix );
		self::validateForm( 'administration/project', 'editProject', $data, $prefix );
		$result	= $model->update( $data );
		if( $result )
			$model->update( array( 'modified' => time() ) );
		return $result;
	}

	public static function getProjectData( $projectId )
	{
		$model	= new Model_Project( $projectId );
		$data	= $model->get();
		if( !$data )
			throw new CMF_Krypton_Exception_Logic( 'invalid_project_id' );
		return $data;
	}
	
	public static function getProjectDescription( $projectId )
	{
		$data	= self::getProjectData( $projectId );
		return $data['description'];
	}
	
	public static function getProjectList( $assoc = false )
	{
		$projects	= array();
		$model	= new Model_Project();
		$list	= $model->find();
		return $list;
	}
	
	public static function getProjects( $assoc = false )
	{
		$list	= array();
		$model	= new Model_Project();
		$list	= $model->find();
		foreach( $list as $entry )
			$list[$entry['projectId']]	= $entry['title'];
		return $list;
	}
	
	public static function getProjectTitle( $projectId )
	{
		$data	= self::getProjectData( $projectId );
		return $data['title'];
	}
	public static function addProjectToServer( $projectId, $serverId )
	{
		$model	= new Model_ProjectServer();
		$model->focusIndex( 'projectId', $projectId );
		$model->focusIndex( 'serverId', $serverId );
		if( !$model->exists() )
		{
			$data	= array(
				'serverId'		=> $serverId,
				'projectId'	=> $projectId,
			);
			return $model->insert( $data );
		}
		return 0;
	}

	public static function getProjectsFromServer( $serverId, $assoc = 0 )
	{
		$model		= new Model_ProjectServer();
		$model->focusIndex( 'serverId', $serverId );
		$relations	= $model->get( FALSE );
		$list		= array();
		foreach( $relations as $relation )
		{
			if( $assoc == 1 )
				$list[$relation['projectId']]	= Logic_Project::getProjectTitle( $relation['projectId'] );
			else if( $assoc == 2 )
				$list[$relation['projectId']]	= Logic_Project::getProjectData( $relation['projectId'] );
			else
				$list[]	= $relation['projectId'];
		}
		return $list;
	}

	public static function getServersFromProject( $projectId, $assoc = 0 )
	{
		$list		= array();
		$model		= new Model_ProjectServer();
		$model->focusIndex( 'projectId', $projectId );
		$relations	= $model->get( FALSE );
		foreach( $relations as $relation )
		{
			if( $assoc == 1 )
				$list[$relation['serverId']]	= Logic_Server::getServerTitle( $relation['serverId'] );
			else if( $assoc == 2 )
				$list[$relation['serverId']]	= Logic_Server::getServerData( $relation['serverId'] );
			else
				$list[]	= $relation['serverId'];
		}
		return $list;
	}

	public static function getServerProjectList( $serverId = 0 )
	{
		$model	= new Model_ProjectServer();
		if( $serverId )
		{
			$model->focusIndex( 'serverId', $serverId );
			$relations	= $model->get( false );
		}
		else
		{
			$relations	= $model->find();
		}
		return $relations;
	}

/*	public static function getServerProjectData( $serverProjectId )
	{
		$model	= new Model_ProjectServer( $serverProjectId );
		$data	= $model->get();
		return $data;
	}*/

	public static function removeProject( $projectId )
	{
		$model	= new Model_Project( $projectId );
		if( !$model->exists() )
			throw new CMF_Krypton_Exception_Logic( 'invalid_project_id' );
		$servers	= Logic_Project::getServersFromProject( $projectId );
		foreach( $servers as $serverId )
			Logic_Project::removeProjectFromServer( $projectId, $serverId );
		$model->delete();
	}

	public static function removeProjectFromServer( $projectId, $serverId )
	{
		$model	= new Model_ProjectServer();
		$model->focusIndex( 'projectId', $projectId );
		$model->focusIndex( 'serverId', $serverId );
		if( $model->exists() )
		{
			return $model->delete();
		}
		return 0;
	}
}
?>
